home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 3 / Example 3.1 / app.cpp next >
Encoding:
C/C++ Source or Header  |  2006-08-01  |  5.2 KB  |  211 lines

  1. //////////////////////////////////////////////////////////////
  2. // Application Framework                                       //
  3. // Written by: C. Granberg, 2005                            //
  4. //////////////////////////////////////////////////////////////
  5.  
  6. #include <windows.h>
  7. #include <d3dx9.h>
  8. #include "debug.h"
  9. #include "intpoint.h"
  10.  
  11. #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  12. #define KEYUP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  13.  
  14. class APPLICATION
  15. {
  16.     public:        
  17.         APPLICATION();
  18.         HRESULT Init(HINSTANCE hInstance, int width, int height, bool windowed);
  19.         HRESULT Update(float deltaTime);
  20.         HRESULT Render();
  21.         HRESULT Cleanup();
  22.         HRESULT Quit();
  23.  
  24.     private:
  25.         IDirect3DDevice9* m_pDevice;
  26.         ID3DXFont *m_pFont;
  27.         HWND m_mainWindow;
  28. };
  29.  
  30. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd)
  31. {
  32.     APPLICATION app;
  33.  
  34.     //Create new window and Initiate Direct3D
  35.     if(FAILED(app.Init(hInstance, 640, 480, true)))
  36.         return 0;
  37.  
  38.     MSG msg;
  39.     memset(&msg, 0, sizeof(MSG));
  40.     int startTime = timeGetTime(); 
  41.  
  42.     //Start the message loop
  43.     while(msg.message != WM_QUIT)
  44.     {
  45.         if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  46.         {
  47.             ::TranslateMessage(&msg);
  48.             ::DispatchMessage(&msg);
  49.         }
  50.         else
  51.         {    
  52.             int t = timeGetTime();
  53.             float deltaTime = (t - startTime)*0.001f;
  54.  
  55.             //For each frame update and render our app
  56.             app.Update(deltaTime);
  57.             app.Render();
  58.  
  59.             startTime = t;
  60.         }
  61.     }
  62.  
  63.     //Cleanup before exit
  64.     app.Cleanup();
  65.  
  66.     return msg.wParam;
  67. }
  68.  
  69. APPLICATION::APPLICATION()
  70. {
  71.     m_pDevice = NULL; 
  72.     m_mainWindow = 0;
  73.     m_pFont = NULL;
  74. }
  75.  
  76. HRESULT APPLICATION::Init(HINSTANCE hInstance, int width, int height, bool windowed)
  77. {
  78.     debug.Print("Application initiated");
  79.  
  80.     //Create Window Class
  81.     WNDCLASS wc;
  82.     memset(&wc, 0, sizeof(WNDCLASS));
  83.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  84.     wc.lpfnWndProc   = (WNDPROC)::DefWindowProc; 
  85.     wc.hInstance     = hInstance;
  86.     wc.lpszClassName = "D3DWND";
  87.  
  88.     //Register Class and Create new Window
  89.     RegisterClass(&wc);
  90.     m_mainWindow = CreateWindow("D3DWND", "Example 3.1: Application Framework", WS_EX_TOPMOST, 0, 0, width, height, 0, 0, hInstance, 0); 
  91.     SetCursor(NULL);
  92.     ShowWindow(m_mainWindow, SW_SHOW);
  93.     UpdateWindow(m_mainWindow);
  94.  
  95.     //Create IDirect3D9 Interface
  96.     IDirect3D9* d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
  97.  
  98.     if(d3d9 == NULL)
  99.     {
  100.         debug.Print("Direct3DCreate9() - FAILED");
  101.         return E_FAIL;
  102.     }
  103.  
  104.     //Check that the Device supports what we need from it
  105.     D3DCAPS9 caps;
  106.     d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
  107.  
  108.     //Hardware Vertex Processing or not?
  109.     int vp = 0;
  110.     if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
  111.         vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
  112.     else vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  113.  
  114.     //Check vertex & pixelshader versions
  115.     if(caps.VertexShaderVersion < D3DVS_VERSION(2, 0) || caps.PixelShaderVersion < D3DPS_VERSION(2, 0))
  116.     {
  117.         debug.Print("Warning - Your graphic card does not support vertex and pixelshaders version 2.0");
  118.     }
  119.  
  120.     //Set D3DPRESENT_PARAMETERS
  121.     D3DPRESENT_PARAMETERS d3dpp;
  122.     d3dpp.BackBufferWidth            = width;
  123.     d3dpp.BackBufferHeight           = height;
  124.     d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
  125.     d3dpp.BackBufferCount            = 1;
  126.     d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
  127.     d3dpp.MultiSampleQuality         = 0;
  128.     d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD; 
  129.     d3dpp.hDeviceWindow              = m_mainWindow;
  130.     d3dpp.Windowed                   = windowed;
  131.     d3dpp.EnableAutoDepthStencil     = true; 
  132.     d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
  133.     d3dpp.Flags                      = 0;
  134.     d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  135.     d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;
  136.  
  137.     //Create the IDirect3DDevice9
  138.     if(FAILED(d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_mainWindow,
  139.                                  vp, &d3dpp, &m_pDevice)))
  140.     {
  141.         debug.Print("Failed to create IDirect3DDevice9");
  142.         return E_FAIL;
  143.     }
  144.  
  145.     //Release IDirect3D9 interface
  146.     d3d9->Release();
  147.  
  148.     //Load Application Specific resources here...
  149.     D3DXCreateFont(m_pDevice, 48, 0, FW_BOLD, 1, false,  
  150.                    DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
  151.                    DEFAULT_PITCH | FF_DONTCARE, "Arial", &m_pFont);
  152.  
  153.  
  154.     return S_OK;
  155. }
  156.  
  157. HRESULT APPLICATION::Update(float deltaTime)
  158. {
  159.     if(KEYDOWN(VK_ESCAPE))
  160.         Quit();
  161.  
  162.     return S_OK;
  163. }    
  164.  
  165. HRESULT APPLICATION::Render()
  166. {
  167.     // Clear the viewport
  168.     m_pDevice->Clear(0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0L);
  169.  
  170.     // Begin the scene 
  171.     if(SUCCEEDED(m_pDevice->BeginScene()))
  172.     {
  173.         //Render scene here...
  174.         RECT r = {0, 0, 640, 480};
  175.  
  176.         m_pFont->DrawText(NULL, 
  177.                           "Hello World!", 
  178.                           -1, 
  179.                           &r, 
  180.                           DT_CENTER | DT_NOCLIP | DT_VCENTER, 
  181.                           0xffffffff);
  182.  
  183.         // End the scene.
  184.         m_pDevice->EndScene();
  185.         m_pDevice->Present(0, 0, 0, 0);
  186.     }
  187.  
  188.     return S_OK;
  189. }
  190.  
  191. HRESULT APPLICATION::Cleanup()
  192. {
  193.     try
  194.     {
  195.         //Release all resources here...
  196.         m_pFont->Release();
  197.         m_pDevice->Release();
  198.  
  199.         debug.Print("Application terminated");
  200.     }
  201.     catch(...){}
  202.  
  203.     return S_OK;
  204. }
  205.  
  206. HRESULT APPLICATION::Quit()
  207. {
  208.     ::DestroyWindow(m_mainWindow);
  209.     ::PostQuitMessage(0);
  210.     return S_OK;
  211. }